//
//  RightViewController.m
//  Custom_Weibo
//
//  Created by LEO on 12/19/15.
//  Copyright © 2015 Leo_Lei. All rights reserved.
//

#import "RightViewController.h"
#import "SettingViewController.h"
#import "BaseNavigationController.h"
#import "PeopleNearbyViewController.h"
@interface RightViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate,
                                  MKMapViewDelegate> {
    ThemeButton *_button;
    CLLocationManager *_locationManager;
    MKMapView *_mapView;
}

@end

@implementation RightViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //  Add custom buttons on the view.
    for (NSInteger i = 0; i < 5; i ++) {
        _button = [ThemeButton buttonWithType:UIButtonTypeCustom];
        _button.frame = CGRectMake(10, 60 * (i + 1), 40, 40);
        _button.defalutImageName = [NSString stringWithFormat:@"newbar_icon_%li", i + 1];
        _button.tag = kWeiboSideButtonTag + i;
        [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_button];
    }
}

- (void)buttonAction:(ThemeButton *)button {
    if (button.tag == kWeiboSideButtonTag) {
        SettingViewController *setting = [[SettingViewController alloc] init];
        BaseNavigationController *baseNavigation = [[BaseNavigationController alloc]
                                                    initWithRootViewController:setting];
        [self presentViewController:baseNavigation animated:YES completion:nil];
    }else if (button.tag == kWeiboSideButtonTag + 1) {
        [self pickPhoto:YES];
    }else if (button.tag == kWeiboSideButtonTag + 2) {
        [self pickPhoto:NO];
    }else if (button.tag == kWeiboSideButtonTag + 4) {
        [self showLocation];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)pickPhoto:(BOOL)isCamera {
    UIImagePickerControllerSourceType sourceType;
    
    //  Make sure whether it turns into album, or open camera.
    if (isCamera) {
        sourceType = UIImagePickerControllerSourceTypeCamera;
        
        //  When you try to use camera, make sure if it is available.
        BOOL isCameraAvailable = [UIImagePickerController isCameraDeviceAvailable:
                                  UIImagePickerControllerCameraDeviceRear];
        if (!isCameraAvailable) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                                message:@"Rear camera is unavailable"
                                                               delegate:self
                                                      cancelButtonTitle:@"Cancel"
                                                      otherButtonTitles:nil];
            [alertView show];
            return;
        }
    }else {
        sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = sourceType;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}


- (void)showLocation {
    PeopleNearbyViewController *people = [[PeopleNearbyViewController alloc] init];
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:people];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 30, 30);
    [backButton setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    people.navigationItem.leftBarButtonItem = backItem;
    
    [self presentViewController:nav animated:YES completion:nil];
}
- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
